package com.wsjc.web.util;

import java.util.List;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Client;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.util.Slowlog;

@Component
public class RedisUtil {


	// 获取redis 服务器信息
	public String getRedisInfo(JedisPool jedisPool) {

		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
		    /*for(int i = 0; i < 100; i++){
		    	jedis.set(Math.random()+"", Math.random()+"");
		    }*/
			Client client = jedis.getClient();
			client.info();
			String info = client.getBulkReply();
			return info;
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		} finally {
			// 返还到连接池
			jedis.close();
		}
	}

	// 获取日志列表
	public List<Slowlog> getLogs(long entries, JedisPool jedisPool) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			List<Slowlog> logList = jedis.slowlogGet(entries);
			return logList;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			// 返还到连接池
			jedis.close();
		}
	}

	// 获取日志条数
	public Long getLogsLen(JedisPool jedisPool) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			long logLen = jedis.slowlogLen();
			return logLen;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			// 返还到连接池
			jedis.close();
		}
	}

	// 清空日志
	public String logEmpty(JedisPool jedisPool) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			return jedis.slowlogReset();
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		} finally {
			// 返还到连接池
			jedis.close();
		}
	}

	// 获取占用内存大小
	public Long dbSize(JedisPool jedisPool) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			// TODO 配置redis服务信息
			Client client = jedis.getClient();
			client.dbSize();
			return client.getIntegerReply();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			// 返还到连接池
			jedis.close();
		}
	}
	
	
	
	// 获取cpu使用情况
	public String useCpuInfo(JedisPool jedisPool) {
		
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			// TODO 配置redis服务信息
			String cpuInfo = jedis.info("CPU");
			return cpuInfo;
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		} finally {
			// 返还到连接池
			jedis.close();
		}
	}
	
	
	// 获取memory使用情况
	public String useMemoryInfo(JedisPool jedisPool) {
		
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			// TODO 配置redis服务信息
			String memoryInfo = jedis.info("Memory");
			return memoryInfo;
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		} finally {
			// 返还到连接池
			jedis.close();
		}
	}

	public String useCmdInfo(JedisPool jedisPool) {
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			// TODO 配置redis服务信息
			String statsInfo = jedis.info("Stats");
			return statsInfo;
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		} finally {
			// 返还到连接池
			jedis.close();
		}
		
	}
	
	
	
	
	
}
